<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * @package			RemaInventoryManagement
 * @subpackage	    PT. RemasysIndo
 * @category		Customized Version
 * @author			Jalu Ahmad Pambudi
 * @mail            j.a.pambudi@gmail.com
 * @copyright		
 * @license			http://remasysindo.com/
 * @link			http://remasysindo.com/remainv/
 */

class M_dailyTrans extends DataMapper {
	var $table = 'stockprice';
	var $key = 'id';
	var $tanggal;
	
	function __construct(){
		parent::__construct();
		$ci=& get_instance();
	}
	
	function getData($tanggal) {
		$sqlStatement = "SELECT a.security_code, b.symbol, c.* 
						FROM stockprice c LEFT OUTER JOIN stockmaster b
						 ON c.stockcode = b.id
						 LEFT OUTER JOIN sekuritas a 
						 ON c.sekuritascode = a.id 
						WHERE c.transdate='".$tanggal."'  ";
		$sqlExecute = $this->db->query($sqlStatement);
		#print $sqlStatement;
		return $sqlExecute->result();
	}
	
	function getSingleData($id) {
		$this->db->get_where($this->table, array('id'=>$id));
			
		return $this->db->result();
	}
	
	function getByDate($tanggal) {
		$this->db->get_where($this->table, array('transdate'=>$tanggal));
		
		$results = false;
		if($this->db->affected_rows()>0) {
		  $results= true;
		}
		
		return $results;
	}
	function getBySekuritas($sekCode) {
		$this->db->get_where($this->table, array('sekuritascode'=>$sekCode));
		
		return $this->db->result();
	}
	
	function getByStock($stockCode) {
		$this->db->get_where($this->table, array('stockcode'=>$stockCode));
	}

	function setupPriceTable($tanggal) {
		
		if (! $check) {
		
		 $sql = " Select id from sekuritas ; " ;
		 $sqlEx = $this->db->query($sql);
		 $sqlFinal = array();
		 foreach($sqlEx->result() as $sekid) {
		  $nsql = "SElect id from stockmaster ; " ;
		  $nsqlEx = $this->db->query($nsql);
		  foreach($nsqlEx->result() as $stockid) {
			$psql = "select max(transdate) as lastdate from stockprice where sekuritascode =". $sekid->id ." and stockcode=".$stockid->id ;
			$psqlEx = $this->db->query($psql);
			$psqlRes = $psqlEx->row();
			$vsql = "SELECT currprice from stockprice where sekuritascode =".$sekid->id ." and stockcode=".$stockid->id ." and transdate='".$psqlRes->lastdate."' ";
			$vsqlEx = $this->db->query($vsql);
			$vsqlRes = $vsqlEx->row();
			if($vsqlEx->num_rows()==1) {
				$lastPrice = $vsqlRes->currprice;
			} else { $lastPrice= '0' ; }
			
			if ($psqlRes->lastdate == $tanggal) {
			} else {
				$sqlFinal =array('stockcode'=>$stockid->id , 'sekuritascode'=>$sekid->id , 'transdate' =>trim($tanggal), 'buyingprice'=>'0', 'lastprice'=>$lastPrice, 'lupd_datetime'=>'now()', 'updatenotes'=>'Update by Dump');
				$this->db->insert('stockprice',$sqlFinal);
			}
		  }
		 }
		}
		#$this->db->insert('stockprice',$sqlFinal);
		#print $isql;
		#print $check;
		#var_dump($sqlFinal);
		#print $tanggal;
		return $check ;
	}	
	 
	function updatePrice($updateData,$id,$tanggal) {
		
		$sqlStatement = "UPDATE $this->table set currprice='".$updateData ."' , updatenotes=' " ;
		$sqlStatement .= "Updated by (updatePriceForm) at ".$tanggal. "' , lupd_datetime=now() where id = ".$id . " and transdate ='".$tanggal."'  ; " ;
		
		$this->db->query($sqlStatement);
		
		#print $sqlStatement;
		return ;
	}
	
	function updateByCutLoss($updateDatat, $id) {
		
	}
	
	function getStockList() {
	  $this->db->select('id,symbol');
	  
	  $sqlEx =$this->db->get('stockmaster');
	  return $sqlEx->result();
	}
	
	function getSekList() {
	  $this->db->select('id,security_code');
	  
	  $sqlEx =$this->db->get('sekuritas');
	  return $sqlEx->result();
	}

	
}
 
?>